package com.shujia.wyh.mr.kqzl2;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class KQZL2Reducer extends Reducer<Text,Text,Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        String dateTime = null;
        String avg = null;
        String city = null;
        //<"2647",["#日期:20180304:26","$白银豫源饭店"]>
        for (Text value : values) {
            String s = value.toString();
            if(s.startsWith("#")){
                String[] split = s.split(":");
                dateTime = split[1];
                avg = split[2];
            }else if(s.startsWith("$")){
                city = s.substring(1);
            }
        }

        if(avg!=null){
            context.write(new Text("时间:"+dateTime+" 监测点编号:"+key.toString()+" 城市:"+city),new LongWritable(Long.parseLong(avg)));
        }


    }
}
